package com.example.redis.demos.web.Redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-07-13
 * Time:10:52
 * Text：无序，随机删除
 */
public class Redis_Set {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("120.46.91.1",6379);
        try(Jedis jedis = jedisPool.getResource()){
            jedis.auth("2287448684t");
            Test2(jedis);
        }
    }
    public static  void Test1(Jedis jedis){
        System.out.println("SADD || SMEMBERS || SISMEMBER || scard");
        jedis.flushAll();
        jedis.sadd("key1","111","222","333");
        Set<String> t1 = jedis.smembers("key1");
        System.out.println("key1当中的元素"+t1+"  长度："+jedis.scard("key1"));
        jedis.spop("key1");
        System.out.println("随机删除一个数："+jedis.smembers("key1")+"  长度："+jedis.scard("key1"));
        boolean result = jedis.sismember("key1","111");
        System.out.println(result);
    }
    public static void Test2(Jedis jedis){
        System.out.println("-----交集sinter-----");

        jedis.flushAll();
        jedis.sadd("key1","111","222","333","444");
        jedis.sadd("key2","aaa","222","ccc","444");

        System.out.println(jedis.sinter("key1","key2"));
        jedis.sinterstore("key3","key1","key2");
        System.out.println(jedis.smembers("key3"));

        System.out.println("----并集-----");
        System.out.println(jedis.sunion("key1","key2"));

        System.out.println("---差集-----");
        System.out.println(jedis.sdiff("key1","key2"));
    }
}
